'Copyright (C) 2004 Christiaan Cable
'
'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 3
'of the License, or (at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details (LICENCE.TXT).

Imports ua

Public Class frmColourValue
    Inherits System.Windows.Forms.Form
    Public EditFlag As Boolean
    Public FormParent As Form
    Public ColourValue As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnColour.Click
        ColorDialog1.Color = Color.FromArgb(CInt(ColourValue))
        ColorDialog1.ShowDialog()
        Me.pbxColour.BackColor = ColorDialog1.Color
        ColourValue = Format(ColorDialog1.Color.ToArgb)
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            'check for valid entries and compulsory fields
            If txtPercentage.Text = "" Or Val(txtPercentage.Text) > 100 Then
                MessageBox.Show("Invalid entry for PERCENTAGE. Number must be between 0 and 100.", "Usage Agent", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                txtPercentage.Focus()
                Exit Sub
            End If

            'check percentage doesn't already exist
            If EditFlag = False Then
                Dim NewUsageColourValue As UsageColourValue
                For Each NewUsageColourValue In CType(FormParent, frmConfiguration).lstUsageColourValue.Items
                    If Val(txtPercentage.Text) = NewUsageColourValue.Percentage Then
                        MessageBox.Show("Invalid entry for PERCENTAGE. This value is in use.", "Usage Agent", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                        txtPercentage.Focus()
                        Exit Sub
                    End If
                Next
            End If

            If ColourValue = "" Then
                MessageBox.Show("Invalid entry for COLOUR", "Usage Agent", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                btnColour.Focus()
                Exit Sub
            End If

            'set values
            If EditFlag = False Then
                'find index
                Dim x As Integer
                For Each UsageColourValue As UsageColourValue In CType(FormParent, frmConfiguration).lstUsageColourValue.Items
                    If UsageColourValue.Percentage > Val(txtPercentage.Text) Then Exit For
                    x += 1
                Next
                'insert
                CType(FormParent, frmConfiguration).lstUsageColourValue.Items.Insert(x, New UsageColourValue(CInt(txtPercentage.Text), ColourValue))

                'set selection
                CType(FormParent, frmConfiguration).lstUsageColourValue.SelectedIndex = CType(FormParent, frmConfiguration).lstUsageColourValue.Items.Count - 1
            Else
                CType(FormParent, frmConfiguration).lstUsageColourValue.Items(CType(FormParent, frmConfiguration).lstUsageColourValue.SelectedIndex) = New UsageColourValue(CInt(txtPercentage.Text), ColourValue)
            End If
            CType(FormParent, frmConfiguration).lstUsageColourValue.Refresh()

            Me.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Usage Agent", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Private Sub txtPercentage_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPercentage.KeyPress
        If Char.IsNumber(e.KeyChar) Or e.KeyChar = Chr(8) Then
            ' Key pressed is number 
            e.Handled = False
        Else
            ' Key pressed is not number 
            e.Handled = True
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

End Class
